<?php

/**
 * This is the model class for table "xyh_activity".
 *
 * The followings are the available columns in table 'xyh_activity':
 * @property string $id
 * @property string $type_id
 * @property string $cat_id
 * @property string $alumni_id
 * @property string $title
 * @property string $banner_url
 * @property string $min_banner_url
 * @property string $start_time
 * @property string $end_time
 * @property string $ctime
 * @property string $mtime
 * @property string $dtime
 * @property string $status
 * @property string $is_del
 * @property string $fav_count
 * @property string $signup_count
 */
class Activity extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Activity the static model class
	 */
        public  $is_need_lottery;
        public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'xyh_activity';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('type_id, alumni_id,ctime, mtime, dtime, fav_count, signup_count', 'length', 'max'=>10),
			array('title, banner_url, min_banner_url', 'length', 'max'=>255),
			array('status, is_del', 'length', 'max'=>1),
                        array('is_hot','numerical'),
                        array('title','required','message'=>'标题不能为空!'),
                        array('start_time','required','message'=>'开始时间不能为空!'),
                        array('end_time','required','message'=>'结束时间不能为空!'),
                        array('cat_id','required','message'=>'活动类型不能为空!'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, type_id, cat_id, alumni_id, title, banner_url, min_banner_url, start_time, end_time, ctime, mtime, dtime, status, is_del, fav_count, signup_count', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
                    
                       
                    'Type' => array(self::BELONGS_TO, 'Type', 'type_id'),
                    'Alumni' => array(self::BELONGS_TO, 'Alumni', 'alumni_id'),
                  // 'ActivityLinkSignup' => array(self::BELONGS_TO, 'ActivityLinkSignup', 'id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'type_id' => 'type_id',
			'cat_id' => 'Cat',
			'alumni_id' => 'Alumni',
			'title' => '标题',
			'banner_url' => 'Banner Url',
			'min_banner_url' => 'Min Banner Url',
			'start_time' => 'Start Time',
			'end_time' => 'End Time',
			'ctime' => 'Ctime',
			'mtime' => 'Mtime',
			'dtime' => 'Dtime',
			'status' => 'Status',
			'is_del' => 'Is Del',
			'fav_count' => 'Fav Count',
			'signup_count' => 'Signup Count',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($AlumniId)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

                //p_e($AlumniId);
                if($AlumniId){
                    $sqlAlumni = "SELECT a.id FROM xyh_alumni as a WHERE a.parent_id={$AlumniId}";
                    $dataArr = Yii::app()->db->createCommand($sqlAlumni)->queryAll();
                    
                    $addC ="alumni_id =$AlumniId";
                    foreach ($dataArr as $v){
                        $addC .= " or alumni_id =$v[id] ";
                    }
                    //p_e($addC);
                    $criteria->addCondition($addC);
                }


                if($this->start_time && $this->end_time){
                    $stime= strtotime($this->start_time);
                    $etime = strtotime($this->end_time);
                    
                    $criteria->addCondition("start_time >=$stime and end_time <=$etime");
                }
                elseif ($this->start_time) 
                {  
                    $stime= strtotime($this->start_time);
                    $criteria->addCondition("start_time >=$stime");
             
                 }
              //  $criteria->with='ActivityLinkSignup';
                
                $criteria->addCondition("is_del=2");
		$criteria->compare('id',$this->id,true);
		$criteria->compare('type_id',$this->type_id,true);
		$criteria->compare('cat_id',$this->cat_id,true);
		$criteria->compare('alumni_id',$this->alumni_id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('banner_url',$this->banner_url,true);
		$criteria->compare('min_banner_url',$this->min_banner_url,true);
		//$criteria->compare('start_time',$this->start_time,true);
		//$criteria->compare('end_time',$this->end_time,true);
		$criteria->compare('ctime',$this->ctime,true);
		$criteria->compare('mtime',$this->mtime,true);
		$criteria->compare('dtime',$this->dtime,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('is_del',$this->is_del,true);
		$criteria->compare('fav_count',$this->fav_count,true);
		$criteria->compare('signup_count',$this->signup_count,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
                        'sort'=>array('defaultOrder'=>'id DESC'),
                        'pagination' => array(
                            'pageSize' => 10, //代表每页显示信息 
                        ),
                       
		));
	}
        
        
        
        
        
        
        public function is_edit($type_id,$id)
        {
            
            switch ($type_id)
            {
                case 101:
                    
                    break;
                
                case 101:

                    break;

                default:
                    break;
            }
            
        }
        
        
         public function Dealname($name)
        {
            // p_e($this->type_id);
            if ($this->type_id == 102){
               
                echo CHtml::link($name,array('customer/viewbranchshop','name'=>$name,'id' => $this->id,),array(
                    'data-placement'=>'left',
                    'data-original-title'=>'点击查看'
                    ));
                }
            else{
                
                echo $name;
            }
                
        }
        
        public function getCat($cat_id){
            $Type = Type::model()->findByPk($cat_id);
            if($Type){
                echo $Type->name;
            }else{
                echo '无';
            }   
        }
        
        
        public function getHot($is_hot){
           
            echo $is_hot==1?'<p class="badge badge-important" >是</p>':'';
     
        }
        
          public function getLottery($id){
           
              $dataArr = ActivityLinkSignup::model()->findByAttributes(array('activity_id'=>$id));
            
              if($dataArr){
                  if($dataArr->is_need_lottery ==1){
                  echo CHtml::link('抽奖设置', array('activity/lottery','id' => $this->id), array('class'=>"badge badge-important"));
                 }
              }
             
            //echo $is_hot==1?'<p class="badge badge-important" >是</p>':'';
     
        }
        
        
        
        public function getCatList($s_default){
            
       $state_data  = Type::model()->findAllByAttributes(array('sign'=>11)); 

        $state = CHtml::listData($state_data, "id", "name");

        return CHtml::dropDownList(__Class__ . '[cat_id]', $s_default, $state, array(
                    'empty' => '全部',
                    'style' => 'width:79px',
                        )
        );
            
                    
        }
        
        
         public function getTypeList($s_default){
            
       $state_data  = Type::model()->findAllByAttributes(array('sign'=>10)); 

        $state = CHtml::listData($state_data, "id", "name");

        return CHtml::dropDownList(__Class__ . '[type_id]', $s_default, $state, array(
                    'empty' => '全部',
                    'style' => 'width:79px',
                        )
        );
            
                    
        }
        
      

}